#!/bin/bash

# This grabs the binaries made in previous steps, and turns them into 'Universal' binaries which can be used on
# either x64 or M1 hardware.
VERSION=$1

mkdir package
mkdir package/x86_64
mkdir package/aarch64

tar -zxvf macos_binaries_*-x86_64-apple-darwin* -C package/x86_64
tar -zxvf macos_binaries_*-aarch64-apple-darwin* -C package/aarch64

rm -f macos_binaries_*

cd package || exit
mkdir final

# Universal binaries seem to have permission problems, so split back to individual builds..
# Tracking: https://github.com/tauri-apps/tauri/issues/8041
#lipo -create -output final/goxlr-client x86_64/goxlr-client aarch64/goxlr-client
#lipo -create -output final/goxlr-daemon x86_64/goxlr-daemon aarch64/goxlr-daemon
#lipo -create -output final/goxlr-launcher x86_64/goxlr-launcher aarch64/goxlr-launcher
#lipo -create -output final/goxlr-defaults x86_64/goxlr-defaults aarch64/goxlr-defaults
#lipo -create -output final/goxlr-initialiser x86_64/goxlr-initialiser aarch64/goxlr-initialiser
#lipo -create -output final/goxlr-utility-ui x86_64/goxlr-utility-ui aarch64/goxlr-utility-ui

echo "Constructing App.."

# Ok, build our .app..
DATE=$(date +"%Y%m%d.%H%M%S")

mkdir 'GoXLR Utility.app'
mkdir -p 'GoXLR Utility.app/Contents/MacOS'
mkdir -p 'GoXLR Utility.app/Contents/Resources'

echo "Using x64 binaries..";
cp x86_64/* 'GoXLR Utility.app/Contents/MacOS'
#cp ../ci/macos/icon.icns 'GoXLR Utility.app/Contents/Resources'
cp ../daemon/resources/icon.icns 'GoXLR Utility.app/Contents/Resources'

# Generate the plist for the App..
sed -e "s/{{VERSION}}/$VERSION/g" ../ci/macos/Info.plist.template.xml | sed -e "s/{{DATE}}/$DATE/g" > 'GoXLR Utility.app/Contents/Info.plist'

# Build the .pkg..
mkdir pkg
mv 'GoXLR Utility.app' pkg/
sed -e "s/{{VERSION}}/$VERSION/g" '../ci/macos/GoXLR Utility.pkgproj' > 'pkg/GoXLR Utility.pkgproj'

mkdir pkg/scripts
cp ../ci/macos/helpers/* pkg/scripts

mkdir pkg/plist
cp ../ci/macos/initialise.mini.plist.xml pkg/plist/com.github.goxlr-on-linux.goxlr-utility.initialiser.mini.plist
cp ../ci/macos/initialise.full.plist.xml pkg/plist/com.github.goxlr-on-linux.goxlr-utility.initialiser.full.plist

cp ../ci/macos/LICENSE pkg/LICENSE.txt

# Run the Package Build..
cd pkg || exit
packagesbuild --project 'GoXLR Utility.pkgproj'
mv 'build/GoXLR Utility.pkg' ../../goxlr-utility-macos-$VERSION-intel.pkg

# Switch with the aarch64 binaries, and re-run the build..
echo "Building with aarch64 binaries..";
rm -rf build/
cp ../aarch64/* 'GoXLR Utility.app/Contents/MacOS'
packagesbuild --project 'GoXLR Utility.pkgproj'
mv 'build/GoXLR Utility.pkg' ../../goxlr-utility-macos-$VERSION-m1.pkg